/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.BankAccount;
import br.com.jr2tecnologia.model.entity.Credit;
import br.com.jr2tecnologia.model.repository.CreditRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class CreditDAO extends GenericDAO<Credit, CreditRepository> implements CreditRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public Credit findById(Long id) {
        return (Credit)em.createNamedQuery("Credit.findByPkCredit").
                setParameter("pkCredit", id).getSingleResult();
    }

    public Credit findByValue(Double value) {
        try{
            return (Credit)em.createNamedQuery("Credit.findByValue").
                setParameter("value", value).getSingleResult();
        }catch(NoResultException e){
            logger.log(Level.WARNING, "Cannot find data");
            throw new DAOException(e);
        }catch(NonUniqueResultException e){
            logger.log(Level.WARNING, "Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

    public List<Credit> list(Credit credit){
        try{
            BankAccount bankAccount = credit.getBankAccount();
            String hql = "select credit from Credit as credit ";
            hql+=" left outer join credit.bankAccount as bankAccount";
            hql+=" left outer join bankAccount.company as company";
            hql+=" where credit.pkCredit = credit.pkCredit";

            if(credit.getValue() != null && credit.getValue() > 0)
                hql+=" and expense.value = :value";
            if(bankAccount != null && bankAccount.getPkBankAccount() != null)
                hql+=" and bankAccount.pkBankAccount = :pkBankAccount";
            if(bankAccount != null && bankAccount.getCompany() != null && bankAccount.getCompany().getPkCompany() != null)
                hql+=" and company.pkCompany = :pkCompany";

            hql+=" order by company.pkCompany, bankAccount.pkBankAccount, credit.pkCredit";

            Query query = em.createQuery(hql);
            if(credit.getValue() != null && credit.getValue() > 0)
                query.setParameter("value", credit.getValue());
            if(bankAccount != null && bankAccount.getPkBankAccount() != null)
                query.setParameter("pkBankAccount", bankAccount.getPkBankAccount());
            if(bankAccount != null && bankAccount.getCompany() != null && bankAccount.getCompany().getPkCompany() != null)
                query.setParameter("pkCompany", bankAccount.getCompany().getPkCompany());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list expense",e);
            throw new DAOException(e);
        }
    }

    public List<Credit> listAll(){
        try{
            return em.createNamedQuery("Credit.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }

}
